// pages/person/releaseService/index.js
const ajax = require('../../../utils/quest.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    max: 300, //最多字数 (根据自己需求改变) 
    currentWordNumber: 0,
    showModalStatus: false,
    checkedData: 0,
    cateList: [],
    specialty: '',
    personalPhotolist: [],
    picList: [],
    arrList: [],
    personalInformation: [],
    categoryId: '', //子类id
    categoryName: '',
    selectData: {}, //选中的子类
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id
    })
    this.getTree()
    if (options.id) {
      this.getDetail(options.id)
    }
  },
  //字数限制 
  inputs: function (e) {
    // 获取输入框的内容
    var value = e.detail.value;
    // 获取输入框内容的长度
    var len = parseInt(value.length);
    this.setData({
      currentWordNumber: len //当前字数 
    });
    //最多字数限制
    if (len > this.data.max) return;
    // 当输入框内容的长度大于最大长度限制（max)时，终止setData()的执行
  },
  bandSpecialty: function () {
    this.showModal()
  },
  // 显示对话框
  showModal: function () {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  //隐藏对话框
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  getTree: function () {
    ajax.sendRequest('/category/getCategoryTree', 'get').then(res => {
      const {
        categoryVOList
      } = res.data
      const isList = []
      for (var i = 0; i < categoryVOList.length; i++) {
        for (var j = 0; j < categoryVOList[i].sonCategoryList.length; j++) {
          // console.log(categoryVOList[i].sonCategoryList[j]);
          categoryVOList[i].sonCategoryList[j].isSelected = false
        }
        isList.push(categoryVOList[i])
      }
      this.setData({
        cateList: isList,
      })
    })
  },
  addressRelicList: function (e) {
    let value = e.currentTarget.dataset.id;
    let categoryName = e.currentTarget.dataset.father.categoryName;
    console.log('value', value);
    console.log('categoryName', categoryName);
    this.setData({
      categoryId: value.id,
      selectData: value,
      categoryName: categoryName
    })
  },
  bandCancel() {
    this.hideModal()
  },
  bandDetermine() {
    const {
      selectData,
      categoryName
    } = this.data
    this.setData({
      specialty: categoryName + '<' + selectData.categoryName
    })
    this.hideModal()
  },
  // 上传
  uploadImages: function () {
    const than = this
    const {
      arrList,
      personalPhotolist
    } = than.data

    wx.chooseImage({
      count: 1,
      sizeType: ['original'], //原图
      sourceType: ['album', 'camera'], //支持选取图片
      success(res) {
        arrList.push(res.tempFiles[0])
        ajax.upLoadFiles(res.tempFiles[0].path).then(res => {
          const personalPhoto = {
            'picUrl': res
          }
          personalPhotolist.push(personalPhoto)
          if (res) {
            than.setData({
              picList: arrList,
              personalPhotolist: personalPhotolist
            })
          } else {
            return wx.showToast({
              title: '系统内部错误，请联系管理员',
              icon: 'none',
            })
          }
        })
      }
    })
  },
  // 删除
  delImages: function (e) {
    const {
      personalPhotolist
    } = this.data
    const that = this
    const index = e.currentTarget.dataset.item
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          personalPhotolist.splice(index, 1)
          that.setData({
            personalPhotolist: personalPhotolist
          })
        } else if (res.cancel) {
          console.log('点击取消了');
          return false;
        }
      }
    })

  },
  formSubmitLogin(e) {
    this.setData({
      personalInformation: e.detail.value
    })
    this.save()
  },
  // 提交函数
  save() {
    const {
      personalInformation,
      categoryId,
      categoryName,
      selectData,
      personalPhotolist,
      specialty,
      id
    } = this.data
    if (!personalInformation.commodityName) {
      return wx.showToast({
        icon: "none",
        title: '请填写商品名称',
      })
    } else if (!personalInformation.commodityQuantity) {
      return wx.showToast({
        icon: "none",
        title: '请填写商品数量',
      })
    } else if (!specialty && !categoryId) {
      return wx.showToast({
        icon: "none",
        title: '请选择品类',
      })
    } else if (!personalInformation.serviceDetails) {
      return wx.showToast({
        icon: "none",
        title: '请填写服务详情',
      })
    } else if (!personalPhotolist.length) {
      return wx.showToast({
        icon: "none",
        title: '请上传照片',
      })
    }
    const reqBody = {
      id: id ? id : null,
      ...personalInformation,
      categoryPid: selectData.pid,
      categoryId: categoryId,
      categoryPName: categoryName,
      categoryName: selectData.categoryName,
      servicePicList: personalPhotolist
    }
    console.log('reqBody', reqBody);
    ajax.sendRequest('/provideService/save', 'post', reqBody).then(res => {
      console.log("res", res);
      wx.showModal({
        title: '提示',
        content: '保存成功',
        success(res) {
          if (res.confirm) {
            wx.redirectTo({
              url: '/pages/person/myService/index'
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    })
  },
  getDetail(id) {
    ajax.sendRequest('/provideService/info/' + id, 'get').then(res => {
      console.log("详情", res);
      const info = res.data.provideServiceVO
      this.setData({
        ...info,
        categoryId: info.categoryId,
        personalPhotolist: info.servicePicList,
        specialty: info.categoryPName + '<' + info.categoryName
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})